package io.lvdaxian.mybatis19.binding;


import io.lvdaxian.mybatis19.session.SqlSession;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 这个类是 生成代理类的工厂（工厂模式）
 *
 * @param <T> 泛型
 * @author lihh
 */
public class MapperProxyFactory<T> {
  private final Class<T> mapperInterface;
  
  private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>();
  
  public MapperProxyFactory(Class<T> mapperInterface) {
    this.mapperInterface = mapperInterface;
  }
  
  public T newInstance(SqlSession sqlSession) {
    final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
    // 参数1  Class Loader 加载器
    // 参数2 代理类 要实现的接口列表
    // 参数3 接口【InvocationHandler】 的实现类
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy);
  }
}
